package jgl.vajra.kingkong.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class PictureUtils {
    // 根据路径获得图片并压缩，返回bitmap用于显示
    private static Bitmap getSmallBitmap(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, 480, 800);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(filePath, options);
    }

    //计算图片的缩放值
    private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }


    //把bitmap转换成Base64字符串
    public static String bitmapToString(String filePath) {
        if (TextUtils.isEmpty(filePath))
            return "";

        Bitmap bm = getSmallBitmap(filePath);
        ByteArrayOutputStream btString = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 40, btString);
        byte[] bytes = btString.toByteArray();
        Log.d("d", "压缩后的大小=" + bytes.length);
        String string = Base64.encodeToString(bytes, Base64.URL_SAFE);
        return string;
    }


    //把Base64字符串转换成bitmap
    public static Bitmap base64ToBitmap(String base64String) {
        byte[] decode = Base64.decode(base64String.trim(), Base64.URL_SAFE);
        Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
        return bitmap;
    }

    /**
     * 文件转Base64
     *
     * @param filePath
     * @return
     */
    public static String fileToBase64(String filePath) {
        if (TextUtils.isEmpty(filePath))return "";

        FileInputStream objFileIs = null;
        try {
            objFileIs = new FileInputStream(filePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        ByteArrayOutputStream objByteArrayOS = new ByteArrayOutputStream();
        byte[] byteBufferString = new byte[1024];
        try {
            for (int readNum; (readNum = objFileIs.read(byteBufferString)) != -1; ) {
                objByteArrayOS.write(byteBufferString, 0, readNum);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String videoData = Base64.encodeToString(objByteArrayOS.toByteArray(), Base64.DEFAULT);

        return videoData;
    }
}
